package com.imot.scheduler;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.imot.pojo.entity.EmaRecord;
import com.imot.pojo.entity.HardwareUsage;
import com.imot.service.IEmaRecordService;
import com.imot.service.IHardwareUsageService;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

/**
 * ClassName: HardUsageTask
 * Description:
 * date: 2023/11/10 17:16
 *
 * @author imot-jiah.wang
 */
@Component
@RequiredArgsConstructor
public class HardUsageTask {
    private final IEmaRecordService emaRecordService;
    private final IHardwareUsageService emaHardService;

    @Scheduled(fixedRate = 5000)
    private void updateUsageTask(){
        LocalDate date = LocalDate.now();
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime start = LocalDateTime.of(date, LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(date, LocalTime.MAX);

        LambdaQueryWrapper<EmaRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.ge(EmaRecord::getTime,start);
        wrapper.le(EmaRecord::getTime,end);
        wrapper.orderByDesc(EmaRecord::getId);
        wrapper.last("limit 1");

        LambdaQueryWrapper<HardwareUsage> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.ge(HardwareUsage::getEndTime,start);
        wrapper1.le(HardwareUsage::getEndTime,end);
        wrapper1.orderByDesc(HardwareUsage::getId);
        wrapper1.last("limit 1");

        EmaRecord emaRecord = emaRecordService.getOne(wrapper);
        HardwareUsage hardwareUsage = emaHardService.getOne(wrapper1);


        if (emaRecord == null)return;
        LocalDateTime recordTime = emaRecord.getTime();

        if (hardwareUsage==null){
            HardwareUsage hardwareUsage1 = new HardwareUsage();
            hardwareUsage1.setStartTime(recordTime);
            hardwareUsage1.setEndTime(recordTime);
            hardwareUsage1.setDuration(0);
        }else {
            LocalDateTime endTime = hardwareUsage.getEndTime();
            Duration duration = Duration.between(endTime, recordTime);
            int minute = (int)duration.toMinutes();
            if (endTime == recordTime){return;}
            else if (minute <= 15){
                LocalDateTime startTime = hardwareUsage.getStartTime();
                hardwareUsage.setEndTime(recordTime);
                Duration duration1 = Duration.between(startTime, recordTime);
                int minutes = (int)duration1.toMinutes();
                hardwareUsage.setDuration(minutes);
                emaHardService.updateById(hardwareUsage);
            }else {
                HardwareUsage hardwareUsage1 = new HardwareUsage();
                hardwareUsage1.setStartTime(recordTime);
                hardwareUsage1.setEndTime(recordTime);
                hardwareUsage1.setDuration(0);
                emaHardService.updateById(hardwareUsage1);
            }
        }
    }
}

